﻿using System.Collections.Generic;
using System.Configuration;
using Keede.RabbitMQ;

namespace Keede.Rabbitmq.HandleService
{
    public class HandleServiceConfig : ConfigurationSection
    {
        [ConfigurationProperty("service")]
        public ServiceConfig Service => (ServiceConfig)this["service"];

        [ConfigurationProperty("applicationInit")]
        public ApplicationInitConfig ApplicationInit => (ApplicationInitConfig)this["applicationInit"];

        [ConfigurationProperty("handlerTypes")]
        public HandlerTypeCollection HandlerTypes => (HandlerTypeCollection)this["handlerTypes"];
    }

    public class ServiceConfig : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name => (string)this["name"];

        [ConfigurationProperty("displayname")]
        public string DisplayName => (string)this["displayname"];

        [ConfigurationProperty("description")]
        public string Description => (string)this["description"];
    }

    public class ApplicationInitConfig : ConfigurationElement
    {
        [ConfigurationProperty("assembly")]
        public string Assembly => (string)this["assembly"];
    }

    [ConfigurationCollection(typeof(HandlerTypesConfig))]
    public class HandlerTypeCollection : ConfigurationElementCollection, IEnumerable<HandlerTypesConfig>
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new HandlerTypesConfig();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            var provider = element as HandlerTypesConfig;
            return provider?.Name;
        }

        public IEnumerator<HandlerTypesConfig> GetEnumerator()
        {
            int count = base.Count;

            for (int i = 0; i < count; i++)
            {
                yield return (HandlerTypesConfig)base.BaseGet(i);
            }
        }
    }

    public class HandlerTypesConfig : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name => (string)this["name"];

        [ConfigurationProperty("handlerAssembly")]
        public string HandlerAssembly => (string)this["handlerAssembly"];
    }
}
